﻿#include "AddUser_admin.h"
#include "ui_AddUser_admin.h"
#include <QDialog>
//AddUser_admin::AddUser_admin(QWidget *parent)
//	: QDialog(parent),ui(new Ui::AddUser_admin)
//{
//	ui->setupUi(this);
//	ui->power_comboBox->addItems({ "customer", "admin" });
//	// 设置密码输入框为密码模式（显示为星号）
//	ui->password_lineEdit->setEchoMode(QLineEdit::Password);
//	ui->password_lineEdit->setPlaceholderText("输入密码");
//
//	// 设置确认密码框为密码模式
//	ui->passwordagain_lineEdit->setEchoMode(QLineEdit::Password); // 添加这行
//	ui->passwordagain_lineEdit->setPlaceholderText("确认密码");   // 添加这行
//}

//优化UI版本--------------------------------------------------------------------------------
AddUser_admin::AddUser_admin(QWidget* parent)
    : QDialog(parent), ui(new Ui::AddUser_admin)
{
    ui->setupUi(this);

    // 设置窗口属性
    setWindowTitle("添加用户");
    setMinimumSize(500, 600);
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

    // 设置全局字体
    QFont globalFont("Microsoft YaHei", 10);
    this->setFont(globalFont);

    // 初始化UI
    setupUI();
}

void AddUser_admin::setupUI()
{
    // 设置主窗口样式
    setStyleSheet(R"(
        QDialog {
            background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                      stop: 0 #f5f7fa, stop: 1 #e4e7ec);
            font-family: 'Microsoft YaHei';
        }
    )");

    // 优化标题样式
    ui->titleLabel->setText("👤 添加新用户");
    ui->titleLabel->setStyleSheet(
        "QLabel {"
        "    color: #2c3e50;"
        "    padding: 15px;"
        "    background-color: transparent;"
        "}"
    );

    // 设置标签样式
    QString labelStyle = R"(
        QLabel {
            color: #34495e;
            font-weight: bold;
            font-size: 10pt;
            padding: 5px 0px;
        }
    )";

    // 应用标签样式
    ui->nameLabel->setStyleSheet(labelStyle);
    ui->aliasLabel->setStyleSheet(labelStyle);
    ui->phoneLabel->setStyleSheet(labelStyle);
    ui->powerLabel->setStyleSheet(labelStyle);
    ui->identityLabel->setStyleSheet(labelStyle);
    ui->passwordLabel->setStyleSheet(labelStyle);
    ui->passwordAgainLabel->setStyleSheet(labelStyle);

    // 设置输入框样式
    QString lineEditStyle = R"(
        QLineEdit {
            padding: 10px 12px;
            border: 2px solid #e0e0e0;
            border-radius: 6px;
            background-color: white;
            color: #2c3e50;
            font-size: 10pt;
            selection-background-color: #3498db;
        }
        QLineEdit:focus {
            border-color: #3498db;
            background-color: #fafcff;
        }
        QLineEdit::placeholder {
            color: #a0a0a0;
        }
    )";

    // 应用输入框样式
    ui->name_lineEdit->setStyleSheet(lineEditStyle);
    ui->alias_lineEdit->setStyleSheet(lineEditStyle);
    ui->phone_lineEdit->setStyleSheet(lineEditStyle);
    ui->identity_lineEdit->setStyleSheet(lineEditStyle);
    ui->password_lineEdit->setStyleSheet(lineEditStyle);
    ui->passwordagain_lineEdit->setStyleSheet(lineEditStyle);

    // 设置占位符文本
    ui->name_lineEdit->setPlaceholderText("请输入姓名");
    ui->alias_lineEdit->setPlaceholderText("请输入用户名");
    ui->phone_lineEdit->setPlaceholderText("请输入手机号码");
    ui->identity_lineEdit->setPlaceholderText("请输入身份证号");
    ui->password_lineEdit->setPlaceholderText("请输入密码");
    ui->passwordagain_lineEdit->setPlaceholderText("请再次输入密码");

    // 设置密码输入框为密码模式
    ui->password_lineEdit->setEchoMode(QLineEdit::Password);
    ui->passwordagain_lineEdit->setEchoMode(QLineEdit::Password);

    // 设置下拉框样式
    QString comboBoxStyle = R"(
        QComboBox {
            padding: 10px 12px;
            border: 2px solid #e0e0e0;
            border-radius: 6px;
            background-color: white;
            color: #2c3e50;
            font-size: 10pt;
            selection-background-color: #3498db;
            selection-color: white;
        }
        QComboBox:focus {
            border-color: #3498db;
        }
        QComboBox::drop-down {
            border: none;
            width: 30px;
        }
        QComboBox::down-arrow {
            image: none;
            border-left: 5px solid transparent;
            border-right: 5px solid transparent;
            border-top: 5px solid #7f8c8d;
        }
        QComboBox QAbstractItemView {
            background-color: white;
            border: 1px solid #e0e0e0;
            border-radius: 6px;
            selection-background-color: #3498db;
            selection-color: white;
            outline: none;
        }
        QComboBox QAbstractItemView::item {
            color: #2c3e50;
            padding: 8px 12px;
        }
        QComboBox QAbstractItemView::item:hover {
            background-color: #f0f0f0;
        }
    )";

    ui->power_comboBox->setStyleSheet(comboBoxStyle);
    ui->power_comboBox->addItems({ "customer", "admin" });

    // 设置权限下拉框的提示文本
    ui->power_comboBox->setItemData(0, "普通用户权限", Qt::ToolTipRole);
    ui->power_comboBox->setItemData(1, "管理员权限", Qt::ToolTipRole);

    // 优化按钮样式
    setupButtons();

    // 添加输入验证
    setupValidation();
}


void AddUser_admin::setupButtons()
{
    // 设置按钮样式
    QString buttonStyle = R"(
        QPushButton {
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 6px;
            padding: 10px 20px;
            font-weight: bold;
            font-size: 10pt;
            min-width: 100px;
        }
        QPushButton:hover {
            background-color: #2980b9;
        }
        QPushButton:pressed {
            background-color: #3498db;
        }
    )";

    QString confirmButtonStyle = R"(
        QPushButton {
            background-color: #27ae60;
            color: white;
            border: none;
            border-radius: 6px;
            padding: 10px 20px;
            font-weight: bold;
            font-size: 10pt;
            min-width: 100px;
        }
        QPushButton:hover {
            background-color: #219653;
        }
        QPushButton:pressed {
            background-color: #27ae60;
        }
    )";

    // 应用按钮样式
    ui->okButton->setText("✅ 确认添加");
    ui->cancelButton->setText("❌ 取消");
    ui->okButton->setStyleSheet(confirmButtonStyle);
    ui->cancelButton->setStyleSheet(buttonStyle);

    // 设置按钮光标
    ui->okButton->setCursor(Qt::PointingHandCursor);
    ui->cancelButton->setCursor(Qt::PointingHandCursor);
}

void AddUser_admin::setupValidation()
{
    // 设置手机号输入验证（只允许数字）
    QRegularExpression phoneRegex("^[0-9]*$");
    QValidator* phoneValidator = new QRegularExpressionValidator(phoneRegex, this);
    ui->phone_lineEdit->setValidator(phoneValidator);

    // 设置身份证号输入验证（数字和X）
    QRegularExpression idCardRegex("^[0-9Xx]*$");
    QValidator* idCardValidator = new QRegularExpressionValidator(idCardRegex, this);
    ui->identity_lineEdit->setValidator(idCardValidator);

    // 实时验证密码匹配
    connect(ui->password_lineEdit, &QLineEdit::textChanged, this, &AddUser_admin::validatePassword);
    connect(ui->passwordagain_lineEdit, &QLineEdit::textChanged, this, &AddUser_admin::validatePassword);
}

void AddUser_admin::validatePassword()
{
    QString password = ui->password_lineEdit->text();
    QString passwordAgain = ui->passwordagain_lineEdit->text();

    if (!passwordAgain.isEmpty() && password != passwordAgain) {
        ui->passwordagain_lineEdit->setStyleSheet(
            "QLineEdit {"
            "    padding: 10px 12px;"
            "    border: 2px solid #e74c3c;"
            "    border-radius: 6px;"
            "    background-color: #ffebee;"
            "    color: #2c3e50;"
            "    font-size: 10pt;"
            "}"
        );
    }
    else {
        ui->passwordagain_lineEdit->setStyleSheet(
            "QLineEdit {"
            "    padding: 10px 12px;"
            "    border: 2px solid #e0e0e0;"
            "    border-radius: 6px;"
            "    background-color: white;"
            "    color: #2c3e50;"
            "    font-size: 10pt;"
            "}"
            "QLineEdit:focus {"
            "    border-color: #3498db;"
            "    background-color: #fafcff;"
            "}"
        );
    }
}

//----------------------------------------------------------------------------------------------------

AddUser_admin::~AddUser_admin()
{
    delete ui;
}

users AddUser_admin::User()
{
    users newUser;
    int count = static_cast<int>(allData->usersQueue.size()) + 1;
    newUser.setUserId(std::to_string(count));
    newUser.setUsername(ui->name_lineEdit->text().toStdString());
    newUser.setDisplayName(ui->alias_lineEdit->text().toStdString());
    newUser.setPassword(ui->password_lineEdit->text().toStdString());
    newUser.setPhone(ui->phone_lineEdit->text().toStdString());
    newUser.setRole(ui->power_comboBox->currentText().toStdString());
    newUser.setIdCard(ui->identity_lineEdit->text().toStdString());

    newUser.setState("1");
    return newUser;
}

bool AddUser_admin::passwordsMatch()
{
	return ui->password_lineEdit->text() == ui->passwordagain_lineEdit->text();
}

void AddUser_admin::setUsername(const QString& username)
{
	ui->name_lineEdit->setText(username);
}
void AddUser_admin::setDisplayName(const QString& displayName)
{
	ui->alias_lineEdit->setText(displayName);
}
void AddUser_admin::setPassword(const QString& password)
{
	ui->password_lineEdit->setText(password);
}
void AddUser_admin::setConfirmPassword(const QString& confirmPassword)
{
	ui->passwordagain_lineEdit->setText(confirmPassword);
}
void AddUser_admin::setPhone(const QString& phone)
{
	ui->phone_lineEdit->setText(phone);
}
void AddUser_admin::setRole(const QString& role)
{
	ui->power_comboBox->setCurrentText(role);
}
void AddUser_admin::setIdCard(const QString& idCard)
{
	ui->identity_lineEdit->setText(idCard);
}
void AddUser_admin::EmptyWords()
{
	ui->password_lineEdit->setPlaceholderText("留空则不修改密码");
	ui->passwordagain_lineEdit->setPlaceholderText("留空则不修改密码");
}


